package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
)

func main() {
	//fmt.Printf("%d", unhex(100))
	fmt.Println(shouldEscape(byte('?')))
Loop:
	for n := 0; n < 10; n++ {
		if n > 5 {
			for i := 0; i < 1000; i++ {
				if i == 100 {
					fmt.Printf("n:%d i:%d", n, i)
					break Loop
				}
			}
		}
		fmt.Println(n)
	}

	fmt.Println("-------------------")
	byteOne := []byte{2, 3}
	byteTwo := []byte{5}
	fmt.Printf("Compre result:%d", Compare(byteOne, byteTwo))
	fmt.Println("-------------------")
	fmt.Printf("BytesToInt:%d", BytesToInt(byteOne))
	fmt.Println("-------------------")
	var t interface{}
	t = "10"
	switch t := t.(type) {
	default:
		fmt.Printf("unexpected type %T\n", t) // %T prints whatever type t has
	case bool:
		fmt.Printf("boolean %t\n", t) // t has type bool
	case int:
		fmt.Printf("integer %d\n", t) // t has type int
	case string:
		fmt.Printf("string %q\n", t) // t has type int
	case *bool:
		fmt.Printf("pointer to boolean %t\n", *t) // t has type *bool
	case *int:
		fmt.Printf("pointer to integer %d\n", *t) // t has type *int
	}

	fmt.Println("------------------")
	n, _ := ReadFull([]byte{1, 3, 4, 7})
	fmt.Printf("ReadFull:%d", n)

}

func unhex(c byte) byte {
	switch {
	case '0' <= c && c <= '9':
		return c - '0'
	case 'a' <= c && c <= 'f':
		return c - 'a' + 10
	case 'A' <= c && c <= 'F':
		return c - 'A' + 10
	}
	return 0
}

func shouldEscape(c byte) bool {
	switch c {
	case ' ', '?', '&', '=', '#', '+', '%':
		return true
	}
	return false
}

func Compare(a, b []byte) int {
	for i := 0; i < len(a) && i < len(b); i++ {
		switch {
		case a[i] > b[i]:
			return 1
		case a[i] < b[i]:
			return -1
		}
	}
	switch {
	case len(a) > len(b):
		return 1
	case len(a) < len(b):
		return -1
	}
	return 0
}

func IntToBytes(n int) []byte {
	x := int32(n)
	bytesBuffer := bytes.NewBuffer([]byte{})
	binary.Write(bytesBuffer, binary.BigEndian, x)
	return bytesBuffer.Bytes()
}

func BytesToInt(b []byte) int {
	bytesBuffer := bytes.NewBuffer(b)

	var x int32
	binary.Read(bytesBuffer, binary.BigEndian, &x)

	return int(x)
}

func ReadFull(buf []byte) (n int, err error) {
	for i := 0; i < len(buf); i++ {
		n += int(buf[i])
	}
	return
}
